home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / kgdb.sun3 / core.c < prev    next >
C/C++ Source or Header  |  1989-08-06  |  12KB  |  521 lines

  1. /* Work with core dump and executable files, for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "param.h"
  22.  
  23. #ifdef USG
  24. #include <sys/types.h>
  25. #include <fcntl.h>
  26. #endif
  27.  
  28. #ifdef COFF_ENCAPSULATE
  29. #include "a.out.encap.h"
  30. #else
  31. #include <a.out.h>
  32. #endif
  33. #ifndef N_MAGIC
  34. #ifdef COFF_FORMAT
  35. #define N_MAGIC(exec) ((exec).magic)
  36. #else
  37. #define N_MAGIC(exec) ((exec).a_magic)
  38. #endif
  39. #endif
  40. #include <stdio.h>
  41. #include <signal.h>
  42. #include <sys/param.h>
  43. #include <sys/dir.h>
  44. #include <sys/file.h>
  45. #include <sys/stat.h>
  46.  
  47. #ifdef UMAX_CORE
  48. #include <sys/ptrace.h>
  49. #else
  50. #include <sys/user.h>
  51. #endif
  52.  
  53. #ifndef N_TXTADDR
  54. #define N_TXTADDR(hdr) 0
  55. #endif /* no N_TXTADDR */
  56.  
  57. #ifndef N_DATADDR
  58. #define N_DATADDR(hdr) hdr.a_text
  59. #endif /* no N_DATADDR */
  60.  
  61. #ifndef COFF_FORMAT
  62. #ifndef AOUTHDR
  63. #define AOUTHDR        struct exec
  64. #endif
  65. #endif
  66.  
  67. extern char *sys_siglist[];
  68.  
  69. extern core_file_command (), exec_file_command ();
  70.  
  71. /* Hook for `exec_file_command' command to call.  */
  72.  
  73. void (*exec_file_display_hook) ();
  74.    
  75. /* File names of core file and executable file.  */
  76.  
  77. char *corefile;
  78. char *execfile;
  79.  
  80. /* Descriptors on which core file and executable file are open.
  81.    Note that the execchan is closed when an inferior is created
  82.    and reopened if the inferior dies or is killed.  */
  83.  
  84. int corechan;
  85. int execchan;
  86.  
  87. /* Last modification time of executable file.
  88.    Also used in source.c to compare against mtime of a source file.  */
  89.  
  90. int exec_mtime;
  91.  
  92. /* Virtual addresses of bounds of the two areas of memory in the core file.  */
  93.  
  94. CORE_ADDR data_start;
  95. CORE_ADDR data_end;
  96. CORE_ADDR stack_start;
  97. CORE_ADDR stack_end;
  98.  
  99. /* Virtual addresses of bounds of two areas of memory in the exec file.
  100.    Note that the data area in the exec file is used only when there is no core file.  */
  101.  
  102. CORE_ADDR text_start;
  103. CORE_ADDR text_end;
  104.  
  105. CORE_ADDR exec_data_start;
  106. CORE_ADDR exec_data_end;
  107.  
  108. /* Address in executable file of start of text area data.  */
  109.  
  110. int text_offset;
  111.  
  112. /* Address in executable file of start of data area data.  */
  113.  
  114. int exec_data_offset;
  115.  
  116. /* Address in core file of start of data area data.  */
  117.  
  118. int data_offset;
  119.  
  120. /* Address in core file of start of stack area data.  */
  121.  
  122. int stack_offset;
  123.   
  124. #ifdef COFF_FORMAT
  125. /* various coff data structures */
  126.  
  127. FILHDR file_hdr;
  128. SCNHDR text_hdr;
  129. SCNHDR data_hdr;
  130.  
  131. #endif /* not COFF_FORMAT */
  132.  
  133. /* a.out header saved in core file.  */
  134.   
  135. AOUTHDR core_aouthdr;
  136.  
  137. /* a.out header of exec file.  */
  138.  
  139. AOUTHDR exec_aouthdr;
  140.  
  141. void validate_files ();
  142. unsigned int register_addr ();
  143.  
  144. /* Call this to specify the hook for exec_file_command to call back.
  145.    This is called from the x-window display code.  */
  146.  
  147. void
  148. specify_exec_file_hook (hook)
  149.      void (*hook) ();
  150. {
  151.   exec_file_display_hook = hook;
  152. }
  153.  
  154. /* The exec file must be closed before running an inferior.
  155.    If it is needed again after the inferior dies, it must
  156.    be reopened.  */
  157.  
  158. void
  159. close_exec_file ()
  160. {
  161.   if (execchan >= 0)
  162.     close (execchan);
  163.   execchan = -1;
  164. }
  165.  
  166. void
  167. reopen_exec_file ()
  168. {
  169.   if (execchan < 0 && execfile != 0)
  170.     {
  171.       char *filename = concat (execfile, "", "");
  172.       exec_file_command (filename, 0);
  173.       free (filename);
  174.     }
  175. }
  176.  
  177. /* If we have both a core file and an exec file,
  178.    print a warning if they don't go together.
  179.    This should really check that the core file came
  180.    from that exec file, but I don't know how to do it.  */
  181.  
  182. void
  183. validate_files ()
  184. {
  185.   if (execfile != 0 && corefile != 0)
  186.     {
  187.       struct stat st_core;
  188.  
  189.       fstat (corechan, &st_core);
  190.  
  191.       if (N_MAGIC (core_aouthdr) != 0
  192. #ifdef sprite
  193.      /*
  194.       * The core_aouthdr in Sprite as generated by gcore is 
  195.       * only a guess at the real exec header. The only thing that
  196.       * should always be right is the text size.
  197.       */
  198.       && (core_aouthdr.a_text != exec_aouthdr.a_text))
  199. #else
  200.       && bcmp (&core_aouthdr, &exec_aouthdr, sizeof core_aouthdr))
  201. #endif
  202.     printf ("Warning: core file does not match specified executable file.\n");
  203.       else if (exec_mtime > st_core.st_mtime)
  204.     printf ("Warning: exec file is newer than core file.\n");
  205.     }
  206. }
  207.  
  208. /* Return the name of the executable file as a string.
  209.    ERR nonzero means get error if there is none specified;
  210.    otherwise return 0 in that case.  */
  211.  
  212. char *
  213. get_exec_file (err)
  214.      int err;
  215. {
  216.   if (err && execfile == 0)
  217.     error ("No executable file specified.\n\
  218. Use the \"exec-file\" and \"symbol-file\" commands.");
  219.   return execfile;
  220. }
  221.  
  222. int
  223. have_core_file_p ()
  224. {
  225.   return corefile != 0;
  226. }
  227.  
  228. static void
  229. files_info ()
  230. {
  231.   char *symfile;
  232.   extern char *get_sym_file ();
  233.  
  234.   if (execfile)
  235.     printf ("Executable file \"%s\".\n", execfile);
  236.   else
  237.     printf ("No executable file\n");
  238.   if (corefile == 0)
  239.     printf ("No core dump file\n");
  240.   else
  241.     printf ("Core dump file \"%s\".\n", corefile);
  242.  
  243.   if (have_inferior_p ())
  244.     printf ("Using the running image of the program, rather than these files.\n");
  245.  
  246.   symfile = get_sym_file ();
  247.   if (symfile != 0)
  248.     printf ("Symbols from \"%s\".\n", symfile);
  249.  
  250. #ifdef FILES_INFO_HOOK
  251.   if (FILES_INFO_HOOK ())
  252.     return;
  253. #endif
  254.  
  255.   if (! have_inferior_p ())
  256.     {
  257.       if (execfile)
  258.     {
  259.       printf ("Text segment in executable from 0x%x to 0x%x.\n",
  260.           text_start, text_end);
  261.       printf ("Data segment in executable from 0x%x to 0x%x.\n",
  262.           exec_data_start, exec_data_end);
  263.       if (corefile)
  264.         printf ("(But since we have a core file, we're using...)\n");
  265.     }
  266.       if (corefile)
  267.     {
  268.       printf ("Data segment in core file from 0x%x to 0x%x.\n",
  269.           data_start, data_end);
  270.       printf ("Stack segment in core file from 0x%x to 0x%x.\n",
  271.           stack_start, stack_end);
  272.     }
  273.     }
  274. }
  275.  
  276. /* Read "memory data" from core file and/or executable file.
  277.    Returns zero if successful, 1 if xfer_core_file failed, errno value if
  278.    ptrace failed. */
  279.  
  280. int
  281. read_memory (memaddr, myaddr, len)
  282.      CORE_ADDR memaddr;
  283.      char *myaddr;
  284.      int len;
  285. {
  286.   if (len == 0)
  287.     return;
  288.  
  289.   if (have_inferior_p ())
  290.       return read_inferior_memory (memaddr, myaddr, len);
  291.   else
  292.       return xfer_core_file (memaddr, myaddr, len);
  293. }
  294.  
  295. /* Write LEN bytes of data starting at address MYADDR
  296.    into debugged program memory at address MEMADDR.
  297.    Returns zero if successful, or an errno value if ptrace failed.  */
  298.  
  299. int
  300. write_memory (memaddr, myaddr, len)
  301.      CORE_ADDR memaddr;
  302.      char *myaddr;
  303.      int len;
  304. {
  305.   if (have_inferior_p ())
  306.     return write_inferior_memory (memaddr, myaddr, len);
  307.   else
  308.     error ("Can write memory only when program being debugged is running.");
  309. }
  310.  
  311. #ifndef XFER_CORE_FILE
  312. /* Read from the program's memory (except for inferior processes).
  313.    This function is misnamed, since it only reads, never writes; and
  314.    since it will use the core file and/or executable file as necessary.
  315.  
  316.    It should be extended to write as well as read, FIXME, for patching files.
  317.  
  318.    Return 0 if address could be read, 1 if not. */
  319.  
  320. int
  321. xfer_core_file (memaddr, myaddr, len)
  322.      CORE_ADDR memaddr;
  323.      char *myaddr;
  324.      int len;
  325. {
  326.   register int i;
  327.   register int val;
  328.   int xferchan;
  329.   char **xferfile;
  330.   int fileptr;
  331.   int returnval = 0;
  332.  
  333.   while (len > 0)
  334.     {
  335.       xferfile = 0;
  336.       xferchan = 0;
  337.  
  338.       /* Determine which file the next bunch of addresses reside in,
  339.      and where in the file.  Set the file's read/write pointer
  340.      to point at the proper place for the desired address
  341.      and set xferfile and xferchan for the correct file.
  342.  
  343.      If desired address is nonexistent, leave them zero.
  344.  
  345.      i is set to the number of bytes that can be handled
  346.      along with the next address.
  347.  
  348.      We put the most likely tests first for efficiency.  */
  349.  
  350.       /* Note that if there is no core file
  351.      data_start and data_end are equal.  */
  352.       if (memaddr >= data_start && memaddr < data_end)
  353.     {
  354.       i = min (len, data_end - memaddr);
  355.       fileptr = memaddr - data_start + data_offset;
  356.       xferfile = &corefile;
  357.       xferchan = corechan;
  358.     }
  359.       /* Note that if there is no core file
  360.      stack_start and stack_end are equal.  */
  361.       else if (memaddr >= stack_start && memaddr < stack_end)
  362.     {
  363.       i = min (len, stack_end - memaddr);
  364.       fileptr = memaddr - stack_start + stack_offset;
  365.       xferfile = &corefile;
  366.       xferchan = corechan;
  367.     }
  368.       else if (corechan < 0
  369.            && memaddr >= exec_data_start && memaddr < exec_data_end)
  370.     {
  371.       i = min (len, exec_data_end - memaddr);
  372.       fileptr = memaddr - exec_data_start + exec_data_offset;
  373.       xferfile = &execfile;
  374.       xferchan = execchan;
  375.     }
  376.       else if (memaddr >= text_start && memaddr < text_end)
  377.     {
  378.       i = min (len, text_end - memaddr);
  379.       fileptr = memaddr - text_start + text_offset;
  380.       xferfile = &execfile;
  381.       xferchan = execchan;
  382.     }
  383.       else if (memaddr < text_start)
  384.     {
  385.       i = min (len, text_start - memaddr);
  386.     }
  387.       else if (memaddr >= text_end
  388.            && memaddr < (corechan >= 0? data_start : exec_data_start))
  389.     {
  390.       i = min (len, data_start - memaddr);
  391.     }
  392.       else if (corechan >= 0
  393.            && memaddr >= data_end && memaddr < stack_start)
  394.     {
  395.       i = min (len, stack_start - memaddr);
  396.     }
  397.       else if (corechan < 0 && memaddr >= exec_data_end)
  398.     {
  399.       i = min (len, - memaddr);
  400.     }
  401.       else if (memaddr >= stack_end && stack_end != 0)
  402.     {
  403.       i = min (len, - memaddr);
  404.     }
  405.       else
  406.     {
  407.       /* Address did not classify into one of the known ranges.
  408.          This shouldn't happen; we catch the endpoints.  */
  409.       fatal ("Internal: Bad case logic in xfer_core_file.");
  410.     }
  411.  
  412.       /* Now we know which file to use.
  413.      Set up its pointer and transfer the data.  */
  414.       if (xferfile)
  415.     {
  416.       if (*xferfile == 0)
  417.         if (xferfile == &execfile)
  418.           error ("No program file to examine.");
  419.         else
  420.           error ("No core dump file or running program to examine.");
  421.       val = lseek (xferchan, fileptr, 0);
  422.       if (val < 0)
  423.         perror_with_name (*xferfile);
  424.       val = myread (xferchan, myaddr, i);
  425.       if (val < 0)
  426.         perror_with_name (*xferfile);
  427.     }
  428.       /* If this address is for nonexistent memory,
  429.      read zeros if reading, or do nothing if writing.
  430.      Actually, we never right.  */
  431.       else
  432.     {
  433.       bzero (myaddr, i);
  434.       returnval = 1;
  435.     }
  436.  
  437.       memaddr += i;
  438.       myaddr += i;
  439.       len -= i;
  440.     }
  441.   return returnval;
  442. }
  443. #endif /* XFER_CORE_FILE */
  444.  
  445. /* My replacement for the read system call.
  446.    Used like `read' but keeps going if `read' returns too soon.  */
  447.  
  448. int
  449. myread (desc, addr, len)
  450.      int desc;
  451.      char *addr;
  452.      int len;
  453. {
  454.   register int val;
  455.   int orglen = len;
  456.  
  457.   while (len > 0)
  458.     {
  459.       val = read (desc, addr, len);
  460.       if (val < 0)
  461.     return val;
  462.       if (val == 0)
  463.     return orglen - len;
  464.       len -= val;
  465.       addr += val;
  466.     }
  467.   return orglen;
  468. }
  469.  
  470. #ifdef REGISTER_U_ADDR
  471.  
  472. /* Return the address in the core dump or inferior of register REGNO.
  473.    BLOCKEND is the address of the end of the user structure.  */
  474.  
  475. unsigned int
  476. register_addr (regno, blockend)
  477.      int regno;
  478.      int blockend;
  479. {
  480.   int addr;
  481.  
  482.   if (regno < 0 || regno >= NUM_REGS)
  483.     error ("Invalid register number %d.", regno);
  484.  
  485.   REGISTER_U_ADDR (addr, blockend, regno);
  486.  
  487.   return addr;
  488. }
  489.  
  490. #endif /* REGISTER_U_ADDR */
  491.  
  492. void
  493. _initialize_core()
  494. {
  495.   corechan = -1;
  496.   execchan = -1;
  497.   corefile = 0;
  498.   execfile = 0;
  499.   exec_file_display_hook = 0;
  500.  
  501.   text_start = 0;
  502.   text_end = 0;
  503.   data_start = 0;
  504.   data_end = 0;
  505.   exec_data_start = 0;
  506.   exec_data_end = 0;
  507.   stack_start = STACK_END_ADDR;
  508.   stack_end = STACK_END_ADDR;
  509.  
  510.   add_com ("core-file", class_files, core_file_command,
  511.        "Use FILE as core dump for examining memory and registers.\n\
  512. No arg means have no core file.");
  513.   add_com ("exec-file", class_files, exec_file_command,
  514.        "Use FILE as program for getting contents of pure memory.\n\
  515. If FILE cannot be found as specified, your execution directory path\n\
  516. is searched for a command of that name.\n\
  517. No arg means have no executable file.");
  518.   add_info ("files", files_info, "Names of files being debugged.");
  519. }
  520.  
  521.